package de.ergin.fuhrparkverwaltung.activities;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import de.ergin.fuhrparkverwaltung.R;
import de.ergin.fuhrparkverwaltung.daten.DatenbankManager;

import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class FahrzeugTuev extends Activity {

	protected DatenbankManager mHelper;
	protected Cursor mCarCursor;
	protected SQLiteDatabase mDatenbank;
	private int mCarID;
	private int nyear;
	private int month;
	private int day;
	private String licensePlate;
	private String mark;
	private String model;
	private String eventDescription;
	private String eventTitel;
	private String eventLocation;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tuev);
		mCarID = getIntent().getIntExtra("carID", 0);
	}

	public void onButtonClickEntry(View view) throws Exception {
		getInformationAboutCar();
		CalendarEntry();
	}

	private void getInformationAboutCar() throws Exception {
		mHelper = new DatenbankManager(this);
		mDatenbank = mHelper.getReadableDatabase();
		RadioButton btn_tuev = (RadioButton) findViewById(R.id.rbtn_Tuev);

		mCarCursor = mDatenbank.rawQuery(
				DatenbankManager.FAHRZEUGE_SELCET_RAW_BY_ID, new String[] { ""
						+ mCarID });
		if (mCarCursor.getCount() == 0) {

			throw new Exception("Fehler kein Fahrzeug vorhanden");

		} else {
			mCarCursor.moveToFirst();
			licensePlate = mCarCursor.getString(mCarCursor
					.getColumnIndex("kennzeichen"));
			mark = mCarCursor.getString(mCarCursor.getColumnIndex("marke"));
			model = mCarCursor.getString(mCarCursor.getColumnIndex("modell"));

		}
		if (btn_tuev.isChecked() == true) {
			eventTitel = getString(R.string.str_fahrzeug_muss_zur_tuev_au_untersuchung);
			eventDescription = getString(R.string.str_fahrzeug_mit_dem_kennzeichen_) + " " + licensePlate+ " - " + mark + " " + model + " " + getString(R.string.str_muss_in_die_tuev_au_abnahme);
			eventLocation = getString(R.string.str_tuev_au);
		} else {
			eventTitel = "Fahrzeug muss in die Werkstatt";
			eventDescription = getString(R.string.str_fahrzeug_mit_dem_kennzeichen_)+ " " + licensePlate
					+ " - " + mark + " " + model + " " + getString(R.string.str_muss_in_die_werkstatt);
			eventLocation = getString(R.string.str_werkstattbesuch);
		}

	}

	public void onButtonClickDate(View view) {
		showDatePickerDialog();
	}

	private void showDatePickerDialog() {
		Calendar mCal = Calendar.getInstance();
		DatePickerDialog dialog = new DatePickerDialog(this,
				this.getOnDataSetListener(), mCal.get(Calendar.YEAR),
				mCal.get(Calendar.MONTH), mCal.get(Calendar.DAY_OF_MONTH));
		dialog.show();
	}

	private OnDateSetListener getOnDataSetListener() {

		OnDateSetListener result = new OnDateSetListener() {
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				nyear = year;
				month = monthOfYear;
				day = dayOfMonth;
				updateEditTextFieldAfterSetDate();
			}
		};
		return result;
	}

	@SuppressLint("SimpleDateFormat")
	private void updateEditTextFieldAfterSetDate() {
		TextView editDate = (TextView) findViewById(R.id.ed_Date);
		Calendar mCal2 = Calendar.getInstance();
		mCal2.set(nyear, month, day);
		editDate.setText(new SimpleDateFormat("yyyy-MM-dd").format(mCal2
				.getTime()));
	}

	private void CalendarEntry() {
		TextView editDate = (TextView) findViewById(R.id.ed_Date);
		if (editDate.length() == 0) {
			Toast.makeText(this, R.string.str_bitte_vorher_das_datum_auswaehlen,
					Toast.LENGTH_LONG).show();
		} else {
			Calendar beginTime = Calendar.getInstance();
			beginTime.set(nyear, month, day, 10, 30);
			Calendar endTime = Calendar.getInstance();
			endTime.set(nyear, month, day, 10, 30);
			Intent intent = new Intent(Intent.ACTION_INSERT,CalendarContract.Events.CONTENT_URI);
			
			
			intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,beginTime.getTimeInMillis());
			intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,endTime.getTimeInMillis());
			intent.putExtra(CalendarContract.Events.TITLE, eventTitel);
			intent.putExtra(CalendarContract.Events.DESCRIPTION, eventDescription);
			intent.putExtra(CalendarContract.Events.EVENT_LOCATION, eventLocation);
			intent.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_FREE);
		  
			startActivity(intent);
			finish();
		}
	}

}
